﻿<!DOCTYPE html>
<html>
<head>
    <title>公告添加</title>
    <link href="../../WF/Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="../../WF/Scripts/bootstrap/js/jquery.min.js" type="text/javascript"></script>
    <script src="../../WF/Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <!--富文本编辑器-->
    <link href="bootstrap-wysiwyg-master/external/google-code-prettify/prettify.css"
        rel="stylesheet" type="text/css" />
    <link href="bootstrap-wysiwyg-master/external/bootstrap-combined.min.css" rel="stylesheet"
        type="text/css" />
    <link href="Font-Awesome-3.0.2/css/font-awesome.css" rel="stylesheet" type="text/css" />
    <script src="bootstrap-wysiwyg-master/external/jquery.hotkeys.js" type="text/javascript"></script>
    <script src="bootstrap-wysiwyg-master/external/google-code-prettify/prettify.js"
        type="text/javascript"></script>
    <script src="bootstrap-wysiwyg-master/bootstrap-wysiwyg.js" type="text/javascript"></script>
    <link href="bootstrap-wysiwyg-master/index.css" rel="stylesheet" type="text/css" />
    <!--选中效果-->
    <!-- 引用通用的js文件. -->
    <script type="text/javascript" src="../../WF/Scripts/config.js"></script>
    <script src="../../WF/Comm/Gener.js" type="text/javascript"></script>
    <style type="text/css">
        #editor
        {
            overflow: scroll;
            max-height: 300px;
        }
    </style>
</head>
<body>
    <!-- <img src="Demo.png" />-->
    <div class="container">
        <!--<div class="page-header">
            <h1>
                添加公告</h1>
        </div>-->
        <form class="form-horizontal" role="form" id="frm_Notice">
        <div class="form-group">
            <label for="name">
                <h4>
                    公告类型:</h4>
            </label>
            <div id="div_NoticeCategory" class="form-group">
            </div>
        </div>
        <div class="form-group">
            <label for="TB_Title">
                <h4>
                    标题</h4>
            </label>
            <input type="text" class="form-control" style="display: inline-block" id="TB_Title"
                name="TB_Title" placeholder="请输入标题" />
        </div>
        <div class="form-group">
            <label for="editor">
                <h4>
                    内容</h4>
            </label>
            <!--   START  富文本器-->
            <div class="hero-unit">
                <div id="alerts">
                </div>
                <!--　 START　工具条-->
                <div class="btn-toolbar" data-role="editor-toolbar" data-target="#editor">
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" title="Font"><i class="icon-font">
                        </i><b class="caret"></b></a>
                        <ul class="dropdown-menu">
                        </ul>
                    </div>
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" title="Font Size"><i class="icon-text-height">
                        </i>&nbsp;<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a data-edit="fontSize 5"><font size="5">Huge</font></a></li>
                            <li><a data-edit="fontSize 3"><font size="3">Normal</font></a></li>
                            <li><a data-edit="fontSize 1"><font size="1">Small</font></a></li>
                        </ul>
                    </div>
                    <div class="btn-group">
                        <a class="btn" data-edit="bold" title="Bold (Ctrl/Cmd+B)"><i class="icon-bold"></i>
                        </a><a class="btn" data-edit="italic" title="Italic (Ctrl/Cmd+I)"><i class="icon-italic">
                        </i></a><a class="btn" data-edit="strikethrough" title="Strikethrough"><i class="icon-strikethrough">
                        </i></a><a class="btn" data-edit="underline" title="Underline (Ctrl/Cmd+U)"><i class="icon-underline">
                        </i></a>
                    </div>
                    <div class="btn-group">
                        <a class="btn" data-edit="insertunorderedlist" title="Bullet list"><i class="icon-list-ul">
                        </i></a><a class="btn" data-edit="insertorderedlist" title="Number list"><i class="icon-list-ol">
                        </i></a><a class="btn" data-edit="outdent" title="Reduce indent (Shift+Tab)"><i class="icon-indent-left">
                        </i></a><a class="btn" data-edit="indent" title="Indent (Tab)"><i class="icon-indent-right">
                        </i></a>
                    </div>
                    <div class="btn-group">
                        <a class="btn" data-edit="justifyleft" title="Align Left (Ctrl/Cmd+L)"><i class="icon-align-left">
                        </i></a><a class="btn" data-edit="justifycenter" title="Center (Ctrl/Cmd+E)"><i class="icon-align-center">
                        </i></a><a class="btn" data-edit="justifyright" title="Align Right (Ctrl/Cmd+R)"><i
                            class="icon-align-right"></i></a><a class="btn" data-edit="justifyfull" title="Justify (Ctrl/Cmd+J)">
                                <i class="icon-align-justify"></i></a>
                    </div>
                    <!--    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" title="Hyperlink"><i class="icon-link">
                        </i></a>
                        <div class="dropdown-menu input-append">
                            <input class="span2" placeholder="URL" type="text" data-edit="createLink" />
                            <button class="btn" type="button">
                                Add</button>
                        </div>
                        <a class="btn" data-edit="unlink" title="Remove Hyperlink"><i class="icon-cut"></i>
                        </a>
                    </div>-->
                    <div class="btn-group">
                        <a class="btn" title="Insert picture (or just drag & drop)" id="pictureBtn"><i class="icon-picture">
                        </i></a>
                        <input type="file" id="picture_select" data-role="magic-overlay" data-target="#pictureBtn"
                            data-edit="insertImage" />
                    </div>
                    <div class="btn-group">
                        <a class="btn" data-edit="undo" title="Undo (Ctrl/Cmd+Z)"><i class="icon-undo"></i>
                        </a><a class="btn" data-edit="redo" title="Redo (Ctrl/Cmd+Y)"><i class="icon-repeat">
                        </i></a>
                    </div>
                    <!--语音输入-->
                    <input type="text" data-edit="inserttext" id="voiceBtn" x-webkit-speech="">
                </div>
                <!--END 工具条-->
                <div id="editor">
                    请输入内容
                </div>
            </div>
        </div>
        <!--  END 富文本器-->
      <!--  <div class="form-group">
            <div class="alert alert-success" role="alert">
                更多设置</div>
        </div>
        <div class="form-group">
            重要程度:
            <label class="radio-inline">
                <input type="radio" name="Importance" id="CB_Importance_0" value="0" checked="checked" />
                普通
            </label>
            <label class="radio-inline">
                <input type="radio" name="Importance" id="CB_Importance_1" value="1" />
                比较重要
            </label>
            <label class="radio-inline">
                <input type="radio" name="Importance" id="CB_Importance_2" value="CB_Importance_2" />
                重要
            </label>
        </div>
        <div class="form-group">
            发布授权:
            <label class="radio-inline">
                <input type="radio" name="Private" id="ck_Private_0" value="ck_SendToPart_0" checked="checked" />
                部门名义
            </label>
            <label class="radio-inline">
                <input type="radio" name="Private" id="ck_Private_1" value="ck_SendToPart_1" />
                个人名义
            </label>
        </div>
        <div class="form-group">
            接收对象:
            <label class="radio-inline">
                <input type="radio" name="Users" id="ck_Users_0" value="0" checked="checked" />
                全体人员
            </label>
            <label class="radio-inline">
                <input type="radio" name="Users" id="ck_Users_1" value="1" />
                部分人员
            </label>
        </div>-->
        <!--start  选择人员弹出窗口-->
        <div class="modal fade" id="myModal" role="basic" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                </div>
            </div>
        </div>
        <!--end  选择人员弹出窗口-->
        <!--<div class="form-group">
            目前状态:
            <label class="radio-inline">
                <input type="radio" name="NoticeSta" id="ck_NoticeSta_0" value="0" checked="checked" />
                自动
            </label>
            <label class="radio-inline">
                <input type="radio" name="NoticeSta" id="ck_NoticeSta_1" value="1" />
                打开
            </label>
            <label class="radio-inline">
                <input type="radio" name="NoticeSta" id="ck_NoticeSta_2" value="2" />
                关闭
            </label>
        </div>-->
        <div class="form-group">
            &nbsp;&nbsp;&nbsp;&nbsp;<label for="inputfile">
                上传附件:</label>
            <input type="file" id="file_AttachFile" name="file_AttachFile" multiple="multiple" />
        </div>
        <button type="submit" class="btn btn-primary btn-lg btn-block">
            添加公告</button>
        </form>
    </div>
</body>
<script type="text/javascript" language="javascript">
    $(function () {
        //通知类型
        Get_NoticeCategory();
        //富文本编辑器
        initToolbarBootstrapBindings();
        $('#editor').wysiwyg({
            fileUploadError: showErrorAlert
        });
        window.prettyPrint && prettyPrint();
        //选择人员
        $("#ck_Users_1").click(function () {

            var url = "../../WF/Comm/RefFunc/BranchesAndLeaf.htm?EnName=BP.OA.Notices&Dot2DotEnsName=BP.OA.NoticeEmps&Dot2DotEnName=BP.OA.NoticeEmp&AttrOfOneInMM=FK_Node&AttrOfMInMM=FK_Emp&EnsOfM=BP.Port.Emps&DefaultGroupAttrKey=FK_Dept";
            // url += "&NodeID=&PKVal=";          
            WinOpen(url, "选择人员");
        });
        //表单提交
        $("#frm_Notice").submit(function () {
            submit();
        });
    });

    //通知类型
    function Get_NoticeCategory() {
        var entities = new Entities("BP.OA.NoticeCategorys");
        entities.RetrieveAll();

        var idx = 0;
        var childRadio;
        var noticeCategoryUI = $("#div_NoticeCategory");
        //遍历通知类型
        noticeCategoryUI.empty();
        $.each(entities, function (index, entity) {
            if (idx == "0")
                childRadio = "<label class='radio-inline'><input type='radio' name='NoticeCategory' id=\"" + entity.No + "\"  value=\"" + entity.No + "\"   checked='checked' >" + entity.Name + "</label>&nbsp;&nbsp;";
            else
                childRadio = "<label class='radio-inline'><input type='radio' name='NoticeCategory' id=\"" + entity.No + "\"  value=\"" + entity.No + "\"  >" + entity.Name + "</label>&nbsp;&nbsp;";

            noticeCategoryUI.append(childRadio);
        });

    }

    //添加公告
    function submit() {
        if ($("#TB_Title").val() == "") {
            alert("标题不能为空");
            return;
        }
        //form表单序列话
        var parasData = $("form").serialize();
        //form表单序列化时调用了encodeURLComponent方法将数据编码了
        parasData = decodeURIComponent(parasData, true);
        parasData = decodeURIComponent(parasData, true);
        parasData = parasData.replace(/TB_/g, '');
        parasData = parasData.replace(/RB_/g, '');
        parasData = parasData.replace(/CB_/g, '');
        parasData = parasData.replace(/DDL_/g, '');

        var formData = new FormData();
        formData.append("AttachFile", $("#file_AttachFile")[0].files[0]);
        formData.append("Doc", $('#editor').cleanHtml());

        var Url = "/App/Notice/Handler.ashx?DoType=Notice_Add&DoMethod=Notice_Add&HttpHandlerName=BP.OA.HttpHandler.App_Notice&" + parasData + "&t=" + new Date().getTime();
        $.ajax({
            url: Url,
            type: 'POST',
            data: formData,
            async: false,
            // 告诉jQuery不要去处理发送的数据
            processData: false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType: false,
            beforeSend: function () {
                console.log("正在进行，请稍候");
            },
            success: function (responseStr) {

                if (responseStr.indexOf('false') == 0) {
                    alert("已存在相同类型、相同标题的公告");
                    return false;
                }
                if (responseStr.indexOf('err@') == 0) {
                    alert("请重新保存");
                    return false;
                }
                confirm("添加成功");
            },
            error: function (responseStr) {
                if (responseStr.indexOf('err@') != -1)
                    alert(responseStr);

                console.log(responseStr);
            }
        });

        
        window.location.href = "List.htm";

    }

    function initToolbarBootstrapBindings() {
        var fonts = ['Serif', 'Sans', 'Arial', 'Arial Black', 'Courier',
            'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande', 'Lucida Sans', 'Tahoma', 'Times',
            'Times New Roman', 'Verdana'],
            fontTarget = $('[title=Font]').siblings('.dropdown-menu');
        $.each(fonts, function (idx, fontName) {
            fontTarget.append($('<li><a data-edit="fontName ' + fontName + '" style="font-family:\'' + fontName + '\'">' + fontName + '</a></li>'));
        });
        $('a[title]').tooltip({ container: 'body' });
        $('.dropdown-menu input').click(function () { return false; })
		    .change(function () { $(this).parent('.dropdown-menu').siblings('.dropdown-toggle').dropdown('toggle'); })
        .keydown('esc', function () { this.value = ''; $(this).change(); });

        $('[data-role=magic-overlay]').each(function () {
            var overlay = $(this), target = $(overlay.data('target'));
            overlay.css('opacity', 0).css('position', 'absolute').offset(target.offset()).width(target.outerWidth()).height(target.outerHeight());
        });
        if ("onwebkitspeechchange" in document.createElement("input")) {
            var editorOffset = $('#editor').offset();
            $('#voiceBtn').css('position', 'absolute').offset({ top: editorOffset.top, left: editorOffset.left + $('#editor').innerWidth() - 35 });
        } else {
            $('#voiceBtn').hide();
        }
    };
    function showErrorAlert(reason, detail) {
        var msg = '';
        if (reason === 'unsupported-file-type') { msg = "Unsupported format " + detail; }
        else {
            console.log("error uploading file", reason, detail);
        }
        $('<div class="alert"> <button type="button" class="close" data-dismiss="alert">&times;</button>' +
		 '<strong>File upload error</strong> ' + msg + ' </div>').prependTo('#alerts');
    };

    /****START  修改*****/

    function uploadImgs(index) {
        // 使用FormData对象上传文件   
        // 这里上传使用递归上传，上传一个再次调用函数本身，再次上传，知道全部上传完成   
        var formData = new FormData();
        if (0 >= index) {
            // 再次上传文本编辑器里的内容   
        }
        else {
            //console.log('执行照片提交。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。');   
            index -= 1;
            formData.append(uploadFiles[index].name, uploadFiles[index]);
            var xhr = new XMLHttpRequest();
            xhr.open("POST", FILEUPLOAD_URL2, true);
            xhr.send(formData);
            xhr.onload = function (data) {
                // 判断图片是否存在          
                var imgTag = [];
                readFileIntoDataUrl(uploadFiles[index]).done(function (dataUrl) {
                    imgTag = $("#editor").find("img[src='" + dataUrl + "']");
                    if (imgTag.length > 0) {
                        // 图片存在, 上传当前文件.               
                        // uploadImage方法为你的上传图片方法.         
                        var url = JSON.parse(data.currentTarget.response).resMsg;
                        imgTag.attr("src", url);
                        // 重新调用函数                    
                        uploadImgs(index);
                    }
                });
            }
        }
    }

    /****END  修改*****/

</script>
</html>
